package com.yq.string;

import java.util.HashMap;

/**
 * @ClassName LengthOfLongestSubstring
 * @Description TODO
 * @Author yaoqiang
 * @Date 2022/4/7 10:03
 * @Version 1.0
 */
public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        String s="pwwkew"; //输出3 "pwwkew" abcabcbb
        System.out.println(lengthOfLongestSubstring(s));
    }

    private static int  lengthOfLongestSubstring(String s) {
        if(s.length()==0) return 0;
        int max=0,left=0;
        HashMap<Character,Integer> map=new HashMap<>();
        int len=s.length();
        for (int i=0;i<len;i++){
            if (map.containsKey(s.charAt(i))){
                //包含当前坐标的时候，说明已经出现了，更新left ,left是最大不变，否则获取hash中存的
                int curLeft=map.get(s.charAt(i))+1;
                left=Math.max(left,curLeft);
                //left=Math.max(left,map.get(s.charAt(i))+1);
            }
            map.put(s.charAt(i),i); //存入当前字符和下标
            max=Math.max(max,i-left+1); //当前窗口长度是当前坐标-左边的+1
        }
        return max;
    }

}
